package com.itgaoshu.hospitalproject.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.*;
import com.itgaoshu.hospitalproject.service.DrugstoreService;
import com.itgaoshu.hospitalproject.service.PharmacyService;
import com.itgaoshu.hospitalproject.service.PutinService;
import com.itgaoshu.hospitalproject.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//包含入库单、库存查询、库存不足、过期提醒4个小标题页面
@Controller
@RequestMapping("seldrugstore")
public class DrugstoreController {
    //库存查询
    @Autowired
    private DrugstoreService drugstoreService;
    @Autowired
    private PutinService putinService;
    @Autowired
    private RecordService recordService;
    //一点击蓝色小标，查看仓库里所有药品种类
    @RequestMapping("selectdgty")
    @ResponseBody
    public Object selectyaopin(Drugdictionary drugdictionary,Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugdictionary> selectyaopin = putinService.selectyaopin(drugdictionary);
        PageInfo pageInfo = new PageInfo(selectyaopin);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //查询选中的药品的库存数量
    @RequestMapping("selnumber")
    @ResponseBody
    public int selnumber(Drugstore drugstore) {
        System.out.print("查询数量方法");
        int selnumber = putinService.selnumber(drugstore);
        System.out.println(selnumber);
        return selnumber;
    }
    //添加一条药品入库
    @RequestMapping("adddrugs")
    @ResponseBody
    public Integer adddrugs(Drugstore drugstore,Record record) {
        System.out.print("查询方法");
        Integer updrugnumber=0;
        Integer adddrugstore=0;
        int seldrugname = putinService.selectdrugsBydrugstorename(drugstore);
        System.out.println(seldrugname+"yyyyyyyyyyyyyyyyyyyyyyyy");
        if(seldrugname==1){
            System.out.print("进入修改方法");
            updrugnumber = putinService.updatedrugs(drugstore);
            System.out.print("添加记录表0");
            int addjilu = recordService.addjilu(record);//添加一条记录
            System.out.println("入库方法成功1"+addjilu);

        }
        if (seldrugname!=1){
            System.out.print("进入添加方法");
            adddrugstore = putinService.adddrugstore(drugstore);
            System.out.print("添加记录表1");
            int addjilu1 = recordService.addjilu(record);//添加一条记录
            System.out.println("入库方法成功"+addjilu1);
        }
        return adddrugstore+updrugnumber;
    }
    //查询库存里的药品清单
    @RequestMapping("selectdrugstore")
    @ResponseBody
    public Object selectAllDrugstoreList(Drugstore drugstore, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugstore> drugstores = drugstoreService.selectAllDrugstoreList();
        PageInfo pageInfo = new PageInfo(drugstores);
        System.out.println(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //编辑药品的时候需要回显的数据
    //查询供货商
    @RequestMapping("selupplier")
    @ResponseBody
    public Object selupplier(Upplier upplier, Model model) {
        List<Upplier> selupplier = drugstoreService.selupplier(upplier);
        System.out.println("这是供货商" + selupplier);
        return selupplier;
    }
    //查询经办人
    @RequestMapping("selskull")
    @ResponseBody
    public Object selskull(Skull skull) {
        List<Skull> selskull = drugstoreService.selskull(skull);
        return selskull;
    }
    //查询药品类型
    @RequestMapping("seltype")
    @ResponseBody
    public Object seltype(Type type) {
        List<Type> seltype = drugstoreService.seltype(type);
        return seltype;
    }
    //查询计量单位
    @RequestMapping("selunit")
    @ResponseBody
    public Object selunit(Unit unit) {
        List<Unit> selunit = drugstoreService.selunit(unit);
        return selunit;
    }
    //查询产地
    @RequestMapping("selarea")
    @ResponseBody
    public Object selarea(Area area) {
        List<Area> selarea = drugstoreService.selarea(area);
        return selarea;
    }
    //编辑药品信息
    @RequestMapping("updrug")
    @ResponseBody
    public Object updateDrugstore(Drugstore drugstore) {
        int result = drugstoreService.updateDrugstore(drugstore);
        if (result > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }
    //库存不足的页面
    //查询仓库里药品数量不足的药品
    @RequestMapping("selectlackdrug")
    @ResponseBody
    public Object selectlackdrug(Drugstore drugstore, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugstore> drugstores = drugstoreService.selectlackdrug(drugstore);//查询药品数量不足的药
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //查询采购单
    @RequestMapping("selcaigou")
    @ResponseBody
    public Object selcaigou(Caigou caigou, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Caigou> caigous = drugstoreService.selcaigou(caigou);//查询遍历采购表
        PageInfo pageInfo = new PageInfo(caigous);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //添加一条药品采购单
    @RequestMapping("addcaigou")
    @ResponseBody
    public Integer addcaigou(Caigou caigou) {
        int selcaigouname = drugstoreService.selcaigouname(caigou);//查询采购单是是否已经存在此条数据
        if(selcaigouname==0){
            int seldrugname =drugstoreService.addcaigou(caigou);//添加
            return seldrugname;
        }else {
            int upcaigounumber = drugstoreService.upcaigounumber(caigou);//修改
            return upcaigounumber;
        }

    }
    //删除一条药品采购单
    @RequestMapping("delcaigou")
    @ResponseBody
    public Integer delcaigou(Integer caigouid) {
        int seldrugname =drugstoreService.delcaigou(caigouid);//删除此条采购数据
        return seldrugname;
    }
    //过期提醒页面
    //查询过期的药
    @RequestMapping("seldrugDateguoqi")
    @ResponseBody
    public Object seldrugDateguoqi(Drugstore drugstore, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugstore> drugstores = drugstoreService.seldrugDate(drugstore);
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //删除过期的药
    @RequestMapping("delguoqidurg")
    @ResponseBody
    public Integer delguoqidurg(Integer rugstoreId,Record record) {
        int delguoqidurg = drugstoreService.delguoqidurg(rugstoreId);
        if(delguoqidurg==1){
          recordService.addjilu(record);
        }
        return delguoqidurg;
    }
}